/*
* $Id: SqlBinder.cpp 54 2010-03-09 20:16:01Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Aleksey
* All rights reserved
*
*/
#include "Common/include.h"
#include "SqlBinder.h"

CSqlBinder::CSqlBinder(const std::tstring& strRequest){
	SetRequest(strRequest);
}
CSqlBinder::CSqlBinder(){}
CSqlBinder::~CSqlBinder(){}

void CSqlBinder::SetValue(int nPos, int value){
	TCHAR strVal[100] = {0};
	_stprintf_s(strVal, 100, _T("%d"), value);
	SetString(nPos, strVal);
}
void CSqlBinder::SetValue(int nPos, const std::tstring& value){
	std::tstring strValue = value + _T("\'");
	size_t pos = 0, disp = 0;
	do{
		strValue.insert(strValue.begin() + pos + disp, '\'');
		disp++;
		if(!pos)
			pos--;
		pos = value.find('\'', pos + 1);
	}while(pos != -1);
	SetString(nPos, strValue);
}
void CSqlBinder::SetValue(int nPos, __int64 value, bool bTime){
	TCHAR strVal[100] = {0};
	if(bTime){
		tm _tm;
		localtime_s(&_tm, &value);
		_tcsftime(strVal, 100, _T("\'%d.%m.%Y %H:%M:%S\'"), &_tm);
	}else
		_stprintf_s(strVal, 100, _T("%d"), value);
	SetString(nPos, strVal);
}
void CSqlBinder::SetValue(int nPos, unsigned int value){
	TCHAR strVal[100] = {0};
	_stprintf_s(strVal, 100, _T("%d"), value);
	SetString(nPos, strVal);
}
void CSqlBinder::SetValue(int nPos, unsigned __int64 value){
	TCHAR strVal[100] = {0};
	_stprintf_s(strVal, 100, _T("%d"), value);
	SetString(nPos, strVal);
}
void CSqlBinder::SetValue(int nPos, double value){
	TCHAR strVal[100] = {0};
	_stprintf_s(strVal, 100, _T("%f"), value);
	SetString(nPos, strVal);
}
void CSqlBinder::SetValue(int nPos, void* value){
	
}
void CSqlBinder::SetValue(int nPos, TCHAR* value){
	SetValue(nPos, std::tstring(value));
}

void CSqlBinder::SetValue(int nPos){
	SetString(nPos, _T("NULL"));
}
void CSqlBinder::SetRequest(const std::tstring& strRequest){
	m_strRequest = strRequest;
	ParseString();
}
void CSqlBinder::ParseString(){
	m_strRequestTemp = m_strRequest;
	bool m_bSlash = false;
	for(size_t i = 0; i < m_strRequestTemp.size(); i++){
		if(m_strRequestTemp[i] == '?'){
			if(!m_bSlash)
				m_aPosition.push_back((int)i);
			else{
				m_strRequestTemp.erase(m_strRequestTemp.begin() + i - 1);
				i--;
			}
		}
		if(m_bSlash)
			m_bSlash = false;
		if(m_strRequestTemp[i] == '\\')
			m_bSlash = true;
	}
}
std::tstring CSqlBinder::GetRequest(){
	std::tstring strRequest = m_strRequestTemp;
	if(m_aParameters.size() != m_aPosition.size())
		return m_strRequest;
	int disp = 0;
	for(std::vector<int>::iterator it = m_aPosition.begin(); it != m_aPosition.end(); it++){
		strRequest.erase(strRequest.begin() + (*it) + disp);
		strRequest.insert(*it + disp, m_aParameters[int(it - m_aPosition.begin())]);
		disp += (int)m_aParameters[int(it - m_aPosition.begin())].size() - 1;
	}
	return strRequest;
}
void CSqlBinder::SetString(int nPos, const std::tstring& value){
	for(std::map<int, std::tstring>::iterator it = m_aParameters.begin(); it != m_aParameters.end(); it++){
		if(it->first == nPos){
			it->second = value;
			return;
		}
	}
	m_aParameters.insert(std::make_pair(nPos, value));
}